package com.bsy.common.decoration

import android.graphics.Canvas
import android.graphics.Paint
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView
import com.bsy.framework.ext.data
import com.bsy.framework.utils.dpToPx

/**
 * Created by Sincerity on 2024/3/18.
 * Copyright
 * 描述：
 */
class ColorDividerItemDecoration(
    private val dividerHeight: Float, @ColorInt color: Int, private val paddingLeft: Int? = 0,
    private val paddingRight: Int? = 0,
    private val paddingTop: Int? = 0,
    private val paddingBottom: Int? = 0
) : RecyclerView.ItemDecoration() {

    private val paint = Paint().apply {
        this.color = color // 设置分隔线颜色
        style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val left = parent.paddingLeft + dpToPx(paddingLeft.data())
        val right = parent.width - parent.paddingRight - dpToPx(paddingRight.data())

        for (i in 0 until parent.childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin + dpToPx(paddingTop.data())
            val bottom = top + dpToPx(dividerHeight) - dpToPx(paddingBottom.data())
            c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
        }
    }
}